'''
@Project ：hdcms-scripts
@File    ：create_course.py
@IDE     ：PyCharm
@Author  ：朱广杰
@Date    ：2024/11/11 14:18
'''
import json

import jsonpath

from script.api.base_api import BaseCmsv2Api
from common.time_util import get_current_datetime, get_next_current_datetime, get_now_timestamp
from common.FILE_PATH import load_yaml_file, BASE_DIR


#  导入课程
class CopyCourseInfoApi(BaseCmsv2Api):
    def __init__(self):
        super().__init__()
        self.common = load_yaml_file('/config/common.yml')
        self.url = f'{self.host}/course/copy_course_info'
        self.method = 'post'
        self.json = {
            "token": self.common['token'],
            "admin_id": self.common['admin_id'],
            "timestamp": get_now_timestamp(),
            "_ignore_sign": "_hundun",
            "course_id": "42260883dddf50be17a8312764248c33",
            "sku_mode": "yxs",
            "tob_remark": ""
        }

# 获取最新创建的课程ID
class GetCourseIdApi(BaseCmsv2Api):
    def __init__(self):
        super().__init__()
        self.common = load_yaml_file('/config/common.yml')
        self.url = f'{self.host}/course/get_course_list'
        self.method = 'get'
        self.params = {
            "token": self.common['token'],
            "admin_id": self.common['admin_id'],
            "timestamp": get_now_timestamp(),
            "_ignore_sign": "_hundun",
            "course_qs": "",
            "course_id": "",
            "sku_mode": "yxs",
            "teacher_qs": "",
            "page_no": 0
        }


# 编辑课程内容
class UpdateCourseApi(BaseCmsv2Api):
    def __init__(self, course_id):
        super().__init__()
        self.common = load_yaml_file('/config/common.yml')
        self.url = f'{self.host}/course/update_course'
        self.method = 'post'
        self.json = {
            "course_id": course_id,
            "course_meta": {
                "title": "脚本创建回放课回放",
                "sku_mode": "yxs",
                "category": "",
                "course_type": 100,
                "live_type": 0,
                "course_score": 5,
                "last_weight_sum": 0,
                "common_can_see": 0,
                "rank": 0,
                "status": 1,
                "school_time": get_current_datetime(),
                "end_time": get_next_current_datetime(),
                "course_note_status": 0,
                "allow_money_buy": 1,
                "money_price": 128,
                "allow_yanzhi_buy": 0,
                "yanzhi_price": 0,
                "recommended_language": "",
                "purchase_url": "",
                "keywords": "",
                "intro_sentence": "",
                "cover_image_pingan": "",
                "language": 0,
                "switch_course": {},
                "course_introduce": "【直面创业真命题，双十一创业者节为创业者赋能】\n\n在这个充满挑战与机遇并存的时代，创业者，依旧是推动社会进步和经济增长的重要力量。作为新时代最具创新精神的群体，他们在市场的波动中前行，在不断的自我突破中寻找属于自己的机会。\n\n为了帮助更多的创业者在不确定的市场环境中找到方向，\n混沌 APP 将在2024年11月2日至11月12日，\n推出为期11天的「双十一创业者节」，\n回应这一群体最关心的市场趋势与创业命题。\n\n本次「双十一创业者节」，以“直面创业真命题”为主题，带来深度解析、案例分享和资源对接，从市场现状、新技术、新人群、到新场景，为创业者们提供了一系列深入的探讨和实践案例，助力创业者在复杂的经济环境中突破自我。\n\n创业从来不是一件简单的事，但每一位敢于直面挑战的创业者，都值得我们致敬。\n我们期待与每一位创业者共同前行，见证你们的伟大时刻。",
                "course_introduce_extend": [
                    {
                        "content": "9:00-9:30 主题分享\n《TikTok的竞争格局和红利机会》\n\n9:35-10:35 创业者圆桌探索流\n《如何抓住TikTok红利实现一盘货卖全球？》\n\n10:35-11:10 互动问答\n\n11:10-12:00 双十一直播间特别活动",
                        "extend_id": "",
                        "title": "课程重点",
                        "images": [
                            {
                                "url": "https://yxs-web.oss-cn-beijing.aliyuncs.com/85720d2594109ad2832b68a8bc9032dc.jpeg?_hd_scale=0.58&_hd_height_scale=33.3333",
                                "scale": 0.58
                            },
                            {
                                "url": "https://yxs-web.oss-cn-beijing.aliyuncs.com/81b7d588f72b95b17825153777ee7224.png?_hd_scale=0.5539143279172821&_hd_height_scale=30.0889",
                                "scale": 0.5539143279172821
                            },
                            {
                                "url": "https://yxs-web.oss-cn-beijing.aliyuncs.com/968b74028bf0cf4c20f50b6360700065.jpeg?_hd_scale=0.58&_hd_height_scale=33.3333",
                                "scale": 0.58
                            }
                        ]
                    }
                ],
                "lt_watch_range": 1,
                "cover_image": "https://yxs-web.oss-cn-beijing.aliyuncs.com/330667d15c58448511a2c14f33432c1e.png",
                "cover_image_v2": "https://yxs-web.oss-cn-beijing.aliyuncs.com/34ae1fec12d97a5a4287cdbe8ddc6f42.png",
                "cover_image_2x1": "https://yxs-web.oss-cn-beijing.aliyuncs.com/08b1b78414160bc880b0c2984e354534.png",
                "cover_image_1x2": "https://yxs-web.oss-cn-beijing.aliyuncs.com/c3c2cc416340390c6df22b5d4d1785d4.png",
                "cover_image_h5": "https://yxs-web.oss-cn-beijing.aliyuncs.com/3554b48e00d7105a5682c5857942e3a9.png",
                "share_info": {
                    "share_h5_switch": 1,
                    "title": "邀你免费学《{课程标题}》",
                    "sub_title": "{老师名}，{老师职位}",
                    "thumbnail": "https://yxs-web.oss-cn-beijing.aliyuncs.com/3a11606e962781b9f48bf936fb07c54b.png?_hd_scale=0.9747292418772563&_hd_height_scale=12.3111",
                    "wxshare_title_sku_member": "{用户名}邀请你，一起来混沌学园听{老师名}老师的课",
                    "poster_img_list": [
                        "https://yxs-web.oss-cn-beijing.aliyuncs.com/1f500e7591a7b03073f4e0e09f87b418.jpeg",
                        "https://yxs-web.oss-cn-beijing.aliyuncs.com/46e24c64f734ea8b382375164087bedc.png"
                    ]
                },
                "relate_courses": [
                    {
                        "module_name": "",
                        "course_ids": []
                    }
                ],
                "wxshare_title_sku_member": "{用户名}邀请你，一起来混沌学园听{老师名}老师的课",
                "share_h5_switch": 1
            },
            "course_resource": {
                "id": "",
                "source_type": "course",
                "android_image": "",
                "android_url": "",
                "ios_image": "",
                "ios_url": ""
            },
            "teacher_list": [
                {
                    "teacher_id": "b3fa8567b984b751f2fc4b4d1e788fef",
                    "teacher_name": "测试老师3",
                    "name": "测试老师3",
                    "course_num": 39,
                    "position": "职位",
                    "head_image": "https://yxs-web.oss-cn-beijing.aliyuncs.com/3a11606e962781b9f48bf936fb07c54b.png?_hd_scale=0.9747292418772563&_hd_height_scale=12.3111",
                    "_index": 0,
                    "_rowKey": 26
                }
            ],
            "course_theme_id": "",
            "course_task_id": "",
            "token": self.common['token'],
            "admin_id": self.common['admin_id'],
            "timestamp": get_now_timestamp(),
            "_ignore_sign": "_hundun"
        }


#  编辑课程视频
class UpdateCourseVideoApi(BaseCmsv2Api):
    def __init__(self, course_id):
        super().__init__()
        self.common = load_yaml_file('/config/common.yml')
        self.url = f'{self.host}/course/update_course_videos'
        self.method = 'post'
        self.json = {
            "course_id": course_id,
            "video_modules": [
                {
                    "module_name": "",
                    "video_list": [
                        {
                            "video_id": "e855910ebff4c4c4f69f8c8e42864fdd",
                            "video_name": "1.为什么选择乌镇案例？",
                            "start_ppt_no": 1,
                            "seq_no": 1,
                            "free_trial": 1,
                            "article_id": "a56e62755837094d4f862f9f5867b5ff",
                            "article_title": "科大讯飞胡郁：如果机器人能有6岁儿童的逻辑水平，将出现爆发式增长【AI营笔记】",
                            "is_share": 0,
                            "share_words": 0
                        },
                        {
                            "video_id": "5b9f55d07514baadf556ff1c47eda07c",
                            "video_name": "2.什么是环境场？",
                            "start_ppt_no": 1,
                            "seq_no": 1,
                            "free_trial": 1,
                            "article_id": "",
                            "article_title": "",
                            "is_share": 0,
                            "share_words": 0
                        },
                        {
                            "video_id": "3271b3efc2edda193a1fee7ae6403f82",
                            "video_name": "3.如何打造行为线索？",
                            "start_ppt_no": 1,
                            "seq_no": 1,
                            "free_trial": 0,
                            "article_id": "",
                            "article_title": "",
                            "is_share": 0,
                            "share_words": 0
                        },
                        {
                            "video_id": "a9c1693e1bc1d3a5c824827e7a4c5eab",
                            "video_name": "4.旅行的本质是什么？",
                            "start_ppt_no": 1,
                            "seq_no": 1,
                            "free_trial": 0,
                            "article_id": "",
                            "article_title": "",
                            "is_share": 0,
                            "share_words": 0
                        },
                        {
                            "video_id": "29bcd5aad11ad6b55f30ff9e04108e3e",
                            "video_name": "5.乌镇案例的启发",
                            "start_ppt_no": 1,
                            "seq_no": 1,
                            "free_trial": 0,
                            "article_id": "",
                            "article_title": "",
                            "is_share": 0,
                            "share_words": 0
                        }
                    ]
                }
            ],
            "token": self.common['token'],
            "admin_id": self.common['admin_id'],
            "timestamp": get_now_timestamp(),
            "_ignore_sign": "_hundun"
        }


#  上传PPT
class UploadPPTApi(BaseCmsv2Api):
    def __init__(self, course_id):
        super().__init__()
        self.common = load_yaml_file('/config/common.yml')
        self.url = f'{self.host}/course/ppt/upload_ppt'
        self.method = 'POST'
        self.headers = {
            "Content-Type": "multipart/form-data; boundary=--------------------------692778717890226814518007"
        }
        self.files = {
            "ppt_pdf": ('default.pdf', open(BASE_DIR + '/data/default.pdf', mode='rb'), 'application/pdf')
        }
        self.json = {
            "token": self.common['token'],
            "admin_id": self.common['admin_id'],
            "_ignore_sign": "_hundun",
            "timestamp": get_now_timestamp(),
            "course_id": course_id,
            "ppt_pdf": ('default.pdf', open(BASE_DIR + '/data/default.pdf', mode='rb'), 'application/pdf'),
            "ppt_type": "2"
        }



if __name__ == '__main__':
    # cci_resp = CopyCourseInfoApi().send()
    gci_resp = GetCourseIdApi().send()
    course_id = jsonpath.jsonpath(gci_resp.json(), '$..course_id')[0]
    course_name = jsonpath.jsonpath(gci_resp.json(), '$..course_title')[0]
    uc_resp = UpdateCourseApi(course_id).send()
    ucv_resp = UpdateCourseVideoApi(course_id).send()


